package com.example.jf.jfapp.fragment.faxian;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.jf.jfapp.DongTaiSquareActivity;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.SaoyisaoActivity;
import com.example.jf.jfapp.activity.NearUserListActivity;
import com.example.jf.jfapp.bean.response.NearUserResponse;
import com.example.jf.jfapp.signal.Find;
import com.example.jf.jfapp.utils.BaiDuLocationUtils;

import java.util.ArrayList;
import java.util.List;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;
import static com.example.jf.jfapp.activity.NearUserListActivity.USER_LIST_KEY;

/**
 * Created by jf on 2017/7/27.
 */

public class FaxianFragment extends Fragment {
    public List<Find>findList=new ArrayList<>();
    ArrayList<NearUserResponse.ResultBean> mResultBeen = new ArrayList<>();


    MapView
            mMapView;
    private BaiduMap mBaiduMap;

    double mLatitude;
    double mLongitude;
private String[] data={"蜂享","附近的人","扫一扫"};



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_faxian, container, false);
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data);
       initFind();
        ListView listView=(ListView)view.findViewById(R.id.list_view);
        FindAdapter adapter1=new FindAdapter(getActivity(),R.layout.item_settings,findList);
        listView.setAdapter(adapter1);

        BaiDuLocationUtils.create(getContext(), new BaiDuLocationUtils.OnLocationListener() {
            @Override
            public void onLocationSucced(BDLocation bdLocation, double latitude, double longitude) {
                mLatitude = latitude;
                mLongitude = longitude;
                Log.e(TAG, "定位成功啦，接受到定位数据" + latitude + "  " + longitude);
                // 构造定位数据
                MyLocationData locData = new MyLocationData
                        .Builder()
                        // 此处设置开发者获取到的方向信息，顺时针0-360
                        .accuracy(bdLocation.getRadius())
                        .direction(0)
                        .latitude(bdLocation.getLatitude())
                        .longitude(bdLocation.getLongitude())
                        .build();
                //mBaiduMap.setMyLocationData(locData);

                LatLng ll = new LatLng(bdLocation.getLatitude(),
                        bdLocation.getLongitude());

                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
//                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Find find=findList.get(position);
                switch (position){
                    case 0:
                        Intent intent0=new Intent(getActivity(), DongTaiSquareActivity.class);
                        startActivity(intent0);

                        break;
                    case 1:
                        Intent intent1=new Intent(getActivity(), NearUserListActivity.class);
                        intent1.putParcelableArrayListExtra(USER_LIST_KEY,mResultBeen);
                        intent1.putExtra("mLatitude",mLatitude);
                        intent1.putExtra("mLongitude",mLongitude);

                        startActivity(intent1);

                        break;
                    case 2:
                        Intent intent2=new Intent(getActivity(), SaoyisaoActivity.class);
                        startActivity(intent2);

                        break;
                }

            }
        });
        return view;
    }
    private void initFind(){
        Find find=new Find(R.drawable.ic_settings,"蜂享");
        findList.add(find);

        Find find1=new Find(R.drawable.ic_near_people,"附近的人");
        findList.add(find1);
        Find find2=new Find(R.drawable.ic_saoyisao,"扫一扫");
        findList.add(find2);
    }

    public class FindAdapter extends  ArrayAdapter<Find>{
        private  int resourceId;
        public FindAdapter(@NonNull Context context, @LayoutRes int resource, List<Find>objects) {
            super(context, resource,objects);
            resourceId=resource;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            Find find=getItem(position);
            View view=LayoutInflater.from(getContext()).inflate(R.layout.item_settings,parent,false);
            ImageView im=(ImageView)view.findViewById(R.id.iv_saoyisao);
            TextView tv=(TextView)view.findViewById(R.id.tv_saoyisao);
            im.setImageResource(find.getImageId());
            tv.setText(find.getName());
            return  view;
        }
    }

//    public void getNearUser(BDLocation location) {
//        //什么时候去请求数据？  定位成功
//        OkHttpClient okHttpClient = new OkHttpClient();
//
//        NearUserRequest nearUserRequest = new NearUserRequest(location.getLatitude(), location.getLongitude());
//
//        final Request request = RequestUtils.requestPOST(nearUserRequest, Constant.URL.GETNEARUSER);
//        okHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String reuslt = response.body().string();
//                Log.i(TAG, "onResponse: " + reuslt);
//                NearUserResponse nearUserResponse = new Gson().fromJson(reuslt, NearUserResponse.class);
//
//            }
//        });
//
//
//    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }




}

